package com.qf.service.impl;

import com.qf.dao.*;
import com.qf.dao.impl.*;
import com.qf.pojo.*;

import com.qf.service.CustomerService;

import java.util.ArrayList;
import java.util.List;

public class CustomerServiceImpl implements CustomerService {
    CustomerDao customerDao = new CustomerDaoImpl();
    CustypeDao custypeDao =new CustypeDaoImpl();

    CustagDao custagDao = new CustagDaoImpl();

    CustateDao cusstateDao =new CustateDaoImpl();

    EmpDao empDao =new EmpDaoImpl();

    TagtypeDao tagtypeDao =new TagtypeDaoImpl();
    //TeamDao teamDao =  new TeamDaoImpl();
    TeamDao teamDao =new TeamDaoImpl();
    @Override
    public PageBean<Customer> findByPage(int pageSize, int pageNum) {
        long totalCount = customerDao.findTotalCount();



        //起始索引
        int startIndex  = (pageNum - 1) * pageSize;

        //查 数据库 获取列表数据
        List<Customer> customerList = customerDao.findListByTypeId(startIndex, pageSize);


        for (Customer customer : customerList) {
            Custype custype= custypeDao.findCustype(customer.getCus_type_id());


            Emp emp= empDao.findEmpName(customer.getEmp_id());

            Cusstate cusstate=cusstateDao.findStateName(customer.getState_id());

            Custag custag=custagDao.findTagType(customer.getTag_id());
            Tagtype tagtype=tagtypeDao.findTypeById(custag.getTagtype_id());

            customer.setCustype(custype);
            customer.setEmp(emp);
            customer.setCustag(custag);
            customer.setCusstate(cusstate);
            customer.setTagtype(tagtype);

        }
        PageBean<Customer> page = new PageBean<>(pageNum,pageSize,totalCount,customerList);
        page.setList(customerList);
        //查 数据库 该分类商品的总数量

        page.setTotalCount(totalCount);

        return page;
    }

    @Override
    public Customer findById(String cusId) {
        Customer customer = customerDao.findCustomer(cusId);
        Custype custype = custypeDao.findCustype(customer.getCus_type_id());
        Emp emp = empDao.findEmpName(customer.getEmp_id());
        Cusstate cusstate = cusstateDao.findStateName(customer.getState_id());
        Custag custag = custagDao.findTagType(customer.getTag_id());
        Tagtype tagtype = tagtypeDao.findTypeById(custag.getTagtype_id());
        Team team = teamDao.findTeamById(customer.getTeam_id());
        customer.setTeam(team);
        custag.setTagtype(tagtype);
        customer.setCustype(custype);
        customer.setEmp(emp);
        customer.setCusstate(cusstate);
        customer.setCustag(custag);
        return customer;


    }



    @Override
    public void addlist(Customer customer) {
        customerDao.insert(customer);
    }

    @Override
    public void doUpdate(Customer customer) {
        customerDao.update(customer);
    }
}
